package com.caiyi.financial.nirvana.sms.service;

import com.alibaba.fastjson.JSON;
import com.caiyi.financial.nirvana.sms.entity.SmsGateway;
import com.caiyi.financial.nirvana.sms.jedis.JedisKey;
import com.caiyi.financial.nirvana.sms.jedis.JedisService;
import com.caiyi.financial.nirvana.sms.queue.ReceiveMsgQueueEntity;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.util.Map;

@Service
public class SmsReceiveMsgService {

    private static Logger logger = LoggerFactory.getLogger(SmsReceiveMsgService.class);

    public String receiveMsg(Map<String, String> param, Integer gatewayId) {
        logger.info("receiveMsg param : {}, gatewayId : {}", param, gatewayId);
        String key = String.format(JedisKey.SMS_GATEWAY, gatewayId);
        String gatewayJson = JedisService.get(key);
        if (StringUtils.isBlank(gatewayJson)) {
            return "ERROR";
        }
        SmsGateway gateway = JSON.parseObject(gatewayJson, SmsGateway.class);
        ReceiveMsgQueueEntity entity = new ReceiveMsgQueueEntity();
        entity.setGatewayId(gatewayId);
        entity.setParam(JSON.toJSONString(param));
        JedisService.lpush(JedisKey.SMS_QUEUE_RECEIVE_MSG, JSON.toJSONString(entity));
        logger.info("receiveMsg param : {}, gatewayId : {}, result : {}", param, gatewayId, gateway.getNotifyResult());
        return gateway.getNotifyResult();
    }
}
